<?php


namespace api\channel\jutuike;


use api\channel\taobao\EleOrderChannel;
use Carbon\Carbon;
use Dml\EasyTBK\Factory;
use easyTbk\taobao\request\EleOfficialactivityGetRequest;
use easyTbk\taobao\request\EleOrderGetRequest;
use think\helper\Arr;

/**
 * 统一拉取订单
 * Class TravelUnion
 * @package api\channel\jutuike
 */
class Union extends Base
{
    //初始化
    public function __construct($serv_type = PRODUCT_TRAVEL_OIL_SERV)
    {
        parent::__construct($serv_type); //暂定拉取聚推客的出行&&后期根据运营再调整
    }

    /**
     *
     * @param $params
     * @return mixed|void
     */
    public function orders($params)
    {
//        优化:同一个秘钥同一个page同一个url就加缓存咯，这样也就是拉一次的效果) https://inews.gtimg.com/newsapp_ls/0/14879756870/0.jiketuchuang.png
        $page = $params['page']??1;
        $url = $this->baseUrl . '/union/orders';
        $appkey = $this->apikey;
        $cacheKey = $url . $params['page'] . $appkey;
        if (cache($cacheKey)) {
            echo '读取缓存:' . $cacheKey . PHP_EOL;
            return cache($cacheKey);
        };

        $filter = [
//            'start_time' => Carbon::now()->subSeconds(3600 * 24 * 360)->format('Y-m-d H:i:s'),
            'start_time' => Carbon::now()->subHours(2)->format('Y-m-d H:i:s'),
            'end_time' => date('Y-m-d H:i:s'),
            'query_type' => 2,    //1： 按支付时间 2：按更新时间 3：创建时间 ，默认：3
            'order_no' => '',
            'order_status' => '',
            'sid' => '',
            'page' => $page,
            'pageSize' => 100,
        ];


        $data = ['apikey' => $this->apikey];

        $data = handleParameters($data, $filter, $params);

        $res = curlPost($url, $data);

        $res = json_decode($res, true);

        if (!empty($res['data'])) cache($cacheKey, $res['data'] ?? [], 55);

        return $res['data'] ?? [];
    }

    /**
     * 产品转化链接
     */
    public function productUrl($args, $act_id)
    {

        $sid = $this->getSid($args['agent_id'], $args['user_id'], $args['channel_id'] ?? '');
        //jutuike 3，和 4 是饿了么的  3 是主推
        if ( in_array($act_id,[3,4])  && env('ELE_TAOBAO',false)){
          return  $this->eleGetUrl($sid);
        }



        $url = $this->baseUrl . '/union/act';

        $longitude = input('longitude','');
        $latitude = input('latitude','');


        $defaults = [
            'sid' => $sid,
            'act_id' => $act_id,
            'longitude' => $longitude,
            'latitude' => $latitude,
        ];

        $data = [
            'apikey' => $this->apikey,
        ];

        if (!empty($args['mobile'])){
            $data['mobile'] = $args['mobile'];
        }

        $data = handleParameters($data, $defaults, $args);

        $res = curlPost($url, $data);
        $res = json_decode($res, true);

        return $res['data'] ?? [];
    }


    public function eleGetUrl($sid){

        $req = new EleOfficialactivityGetRequest($sid);//获取

        $taobao = Factory::taobao([
            'app_key' => EleOrderChannel::APPKEY,
            'app_secret' => EleOrderChannel::SECRET,
            'format' => 'json'
        ]);

        $data = $taobao->execute($req);
        $data = json_decode(json_encode($data), true);

        $data = $data['data'];
        $link = $data['link'];

//        return $data;


        $result =[
            'h5' => $link['h5_short_link']??'',
            'long_h5' => $link['h5_url'] ?? '',
            'we_app_info' => [
                'app_id' => $link['wx_appid']??'',
                'page_path' => $link['wx_path']??'',
                'miniCode' => $link['mini_qrcode']??'',
            ],
            'alipay_app_info' => [
                'app_id' => '2021001110676437',
                'page_path' => $link['alipay_mini_url']??'',
            ],
            'poster_qrcode_url' => '',
            'original_id' => 'gh_6506303a12bb',
            'act_name' => $data['description']??'',
        ];



        return $result;
    }


//{
//"h5": "https://u.ele.me/utjFVbHV",
//"long_h5": "https://fc.ele.me/a/MWJhZDM1OTY5ZTg5MTFlYzllYWEwMDE2M2UxM2FkMTE=?scene=54ee03d47cd44b4ab556ae94c3b13d97",
//"we_app_info": {
//"app_id": "wxece3a9a4c82f58c9",
//"page_path": "commercialize/pages/taoke-guide/index?scene=54ee03d47cd44b4ab556ae94c3b13d97",
//"miniCode": "https://img.alicdn.com/imgextra/i1/6000000000883/O1CN01hxnAPM1IOSd6joJeL_!!6000000000883-0-o2oad.jpg"
//},
//"alipay_app_info": {
//    "app_id": "2021001110676437",
//            "page_path": "plugin-private://2021002141657015/pages/index-bdlm/index?configKey=BDLM_ELE_ALIPAY_DG&scene=54ee03d47cd44b4ab556ae94c3b13d97"
//        },
//        "poster_qrcode_url": "https://img.alicdn.com/imgextra/i1/6000000001510/O1CN01cNZXQP1N1cxXgBeRN_!!6000000001510-0-o2oad.jpg",
//        "original_id": "gh_6506303a12bb",
//        "act_name": "饿了么天天领红包"
//    }




}